****Figure 2****

clear all

use "j&s.dta"

qui tab r2b, gen(r2bd)

foreach var of varlist r2bd1-r2bd580 {
   egen m_`var'= mean(`var')
   gen dm_m_`var'=`var'-m_`var'
   drop  m_`var' 
   } 

   bysort code_delito: egen inc_score=mean(sent_forjail)
   egen mean_inc=mean(sent_forjail)	
   egen temp1=mean(inc_score)
   gen dm_inc_score=inc_score-temp1
   drop temp1
   
 reg sent_forjail  dm_m_r2bd*  , noconst 
 reg sent_forjail dm_inc_score   dm_m_r2bd*, noconst   
 predict res_r4 if e(sample), resid
 
bysort   judge_merge : egen sumtemp1=total( res_r4) if e(sample)
bysort   judge_merge : egen count_temp1=count( res_r4)  if e(sample)
gen pi_3=(sumtemp1- res_r4)/(count_temp1-1) 
gen pib_3=(sumtemp1)/(count_temp1) 
xtile pits4 =  pi_3  , nq(100)

lpoly res_r4 pi_3 if pits4>1 &pits4<99 , graphregion(color(white)) bgcolor(white) ci yaxis(1) xtitle(PI) title("Incarceration") ///
noscatter  level(90) legend(off) ytitle(Prob. Incarceration, axis(1)) ///
addplot(hist  pi_3 if  pits4>1 & pits4<99 , yaxis(2) bcolor(none) ) saving(grph_fs_ifinal.gph, replace )

clear all

use "spoa.dta"

egen r2b=group(rand_level year_sent) if rand_level!=. & year_sent!=.
qui tab r2b, gen(r2bd)
 

foreach var of varlist r2bd1-r2bd836 {
   egen m_`var'= mean(`var')
   gen dm_m_`var'=`var'-m_`var'
   drop  m_`var' 
   } 
   bysort code_delito: egen conv_score=mean(condenado)
   egen mean_con=mean(condenado)
   egen temp1=mean(conv_score)
   gen dm_con_score=conv_score-temp1
   drop temp1
      
 reg condenado dm_m_r2bd* dm_con_score, noconst 
 predict res_ri4, resid
 bysort   judge_merge : egen sumtemp1=total(res_ri4) if e(sample)
bysort   judge_merge : egen count_temp1=count(res_ri4)  if e(sample)
gen pc_3 =(sumtemp1-res_ri4)/(count_temp1-1) 
xtile pctri4 = pc_3 , nq(100)

lpoly res_ri4 pc_3 if  pctri4>1 & pctri4<99 , graphregion(color(white)) bgcolor(white) ci yaxis(1) xtitle(Pc) title("Conviction") ///
noscatter  level(90) legend(off) ytitle(Prob. Conviction, axis(1)) ///
addplot(hist  pc_3  if   pctri4>1 & pctri4<99, yaxis(2) bcolor(none) ) saving(pc_4final.gph, replace )

graph combine  pc_4final.gph grph_fs_ifinal.gph, title(First stage)    graphregion(color(white))  saving(grph_fs, replace)
graph export grph_fs.pdf, replace

****************
****Figure 3****
****************
clear
use "j&s.dta"

scatter  pi_3 pc_3 , msize(tiny) graphregion(color(white)) bgcolor(white) ///
title(Judges' Fixed Effects) ytitle("Incarceration Leniency Conditional on Conviction") ///
xtitle(Conviction Leniency) jitter(0.7) saving(pcpi_apr, replace)
graph export pcpi_apr.pdf, replace



****************
****Figure 4****
****************
use "data.dta"

bysort idglobal: egen first_sisben=min(sisben_year)


gen sex=1 if sisben_sexo==1 
replace  sex=0 if sisben_sexo==2 
gen head=1 if sisben_parentesco==1
replace head=0 if sisben_parentesco!=1 & sisben_parentesco!=.


gen working=1 if sisben_actividad==1
replace working=0 if sisben_actividad!=1 & sisben_actividad!=.
gen studying=1 if sisben_actividad==3
replace studying=0 if sisben_actividad!=3 & sisben_actividad!=.
gen unemp_noac=1 if sisben_actividad==0 | sisben_actividad==2 
replace unemp_noac=0 if sisben_actividad!=0 | sisben_actividad!=2 & sisben_actividad!=.
gen maleandhhh=0
 replace maleandhhh=1 if sisben_sexo==1  & sisben_parentesco==1
 bysort id_hogar_sisben: egen maleh=max(maleandhhh)
gen yeduc=0 if sisben_niveleduc==0
replace yeduc=sisben_grado if sisben_niveleduc==1 & sisben_grad!=.
replace yeduc=(sisben_grado+5) if sisben_niveleduc==2 & sisben_grad!=.
replace yeduc=(sisben_grado+11) if sisben_niveleduc>2 & sisben_niveleduc!=. & sisben_grad!=.
gen hs=1 if yeduc>=11 & yeduc!=.
replace hs=0 if yeduc <11
gen single =1 if sisben_estcivil==5
replace single=0 if sisben_estcivil==1 | sisben_estcivil==2 | sisben_estcivil==3  | sisben_estcivil==4  
bysort id_hogar_sisben: gen temp=yeduc if sisben_parentesco==1
bysort id_hogar_sisben: egen yeduc_hh=median(yeduc)
drop temp
gen hs_completed=0 if finaledu<11 & yob>=1988 & yob<=2000
replace hs_completed=1 if finaledu==11 &  yob>=1988 & yob<=2000
gen mcr_hh=1 if peoplecrhhi>1 & peoplecrhhi!=.
replace mcr_hh=0 if peoplecrhhi==1
bysort id_hogar_sisben: egen temp=min(year_j) if  merge_fam_crime==3
drop if ( year_j>temp  & merge_fam_crime==3) 
drop peoplecrhhi peoplecr peoplecrhh
bysort id_hogar_sisben: egen peoplecr=count(id) if  merge_fam_crime==3
bysort id_hogar_sisben: egen peoplecrhh=max(peoplecr) 
bysort idglobal: egen peoplecrhhi=max(peoplecrhh) 
drop temp
bysort id_hogar_sisben: egen temp=min(yearcrime) if  merge_fam_crime==3 
drop if ( yearcrime>temp  & merge_fam_crime==3 & peoplecrhhi>1 & yearcrime!=.) 
drop peoplecrhhi peoplecr peoplecrhh
bysort id_hogar_sisben: egen peoplecr=count(id) if  merge_fam_crime==3
bysort id_hogar_sisben: egen peoplecrhh=max(peoplecr) 
bysort idglobal: egen peoplecrhhi=max(peoplecrhh) 
bysort idglobal: egen miny=min(sisben_year)
bysort idglobal: egen maxy=max(sisben_year)
bysort idglobal: gen both=1 if miny==2005 & maxy==2010
bysort idglobal: replace both=0 if miny!=2005 | maxy!=2010
tab sisben_year, gen (sisben_yd)
foreach var in   datesent1    yearcrime crime_cat crime_code  {
bysort id_hogar_sisben: egen hh_`var'=median(`var') 
}
foreach var in   datesent1   yearcrime crime_cat crime_code {
bysort id_hogar_sisben: egen hh_`var't=median(`var') 
bysort idglobal: egen hh_`var't2=median(hh_`var't) if multiplehhid==1
bysort idglobal: replace hh_`var'=hh_`var't2 if hh_`var'==. & hh_`var't2!=.
}
gen age_attcrime=hh_yearcrime-yob
replace sisben_sexo=0 if sisben_sexo==2
 foreach var in sisben_sexo  yeduc yob  {
gen temp5=`var' if merge_fam_crime==3
bysort id_hogar_sisben: egen `var'_presoh=median(temp5) if    peoplecrhhi==1 
drop temp* 
}
foreach var in sisben_sexo  yeduc yob  {
gen temp5=`var' if merge_fam_crime==3
bysort id_hogar_sisben: egen `var'_presoht=median(temp5) if    peoplecrhhi==1 
bysort idglobal: egen `var'_presoht2=median(`var'_presoht) if multiplehhid==1
bysort idglobal: replace `var'_presoh=`var'_presoht2 if `var'_presoh==. & `var'_presoht2!=.
drop temp* 
}
gen jailk14=1 if gjail0==1 & hh_datesent1<(yob+14)
replace jailk14=0 if gjail0==0 & hh_datesent1<(yob+14)

foreach var in sisben_puntaje   yeduc_hh yeduc   sisben_parentesco sisben_actividad  maleh   {
gen temp`var'=`var' if sisben_year==2005 
bysort idglobal: egen  `var'2005=median(temp`var')
drop  temp`var'
gen temp`var'=`var' if sisben_year==2010  
bysort idglobal: egen  `var'2010=median(temp`var')
drop  temp`var'
}  
egen judge_g= group(judge_merge) if judge_merge!="."

gen crimis_head=0 if merge_fam_crime==3 & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace crimis_head=1 if merge_fam_crime==3 & (sisben_parentesco2005==1  |  sisben_parentesco2010==1 )
gen crimis_spouse=0 if merge_fam_crime==3 & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace crimis_spouse=1 if merge_fam_crime==3 & (sisben_parentesco2005==2  |  sisben_parentesco2010==2 )
gen crimis_son=0 if merge_fam_crime==3 & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace crimis_son=1 if merge_fam_crime==3 & (sisben_parentesco2005==3  |  sisben_parentesco2010==3 )

foreach var in crimis_head crimis_spouse crimis_son {
bysort id_hogar_sisben: egen hh_`var'=median(`var') 
}
 
gen spouse_ofcrim=0 if merge_fam_crime!=3  & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace spouse_ofcrim=1  if merge_fam_crime!=3  & (hh_crimis_head==1 & sisben_parentesco2005==2) | (hh_crimis_spouse==1 & sisben_parentesco2005==1) |  (hh_crimis_son==1 & sisben_parentesco2005==7)
gen son_ofcrim=0 if merge_fam_crime!=3  & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace son_ofcrim=1  if merge_fam_crime!=3  & (hh_crimis_head==1 & (sisben_parentesco2005==3 | sisben_parentesco2010==3) ) | (hh_crimis_spouse==1 & (sisben_parentesco2005==3 | sisben_parentesco2010==3 )) |  (hh_crimis_son==1 & (sisben_parentesco2005==4 | sisben_parentesco2010==4 ))
  xtile pct=pc_3, nq(100)
  tab hh_crime_cat, gen(ccatd)
gen hh_jail=gjail0
gen hh_jail1=gjail1
xtile pc_raw_tile=pc_raw, nq(3)
foreach var in sent_year pc_raw  pi_2 pib_3 pits4b pc_3 pc_1   pct1 pi_3 pits4 pc_tile3 pct inc_score corte  r2b   year_j num_jueces judge_g rand_level ncrime penatot_m sentl_valid {
bysort id_hogar_sisben: egen hh_`var'=median(`var') 
}

foreach var in sent_year pc_raw  pct2_100 pc_tile2 pc_raw100 pc_raw_tile  pi_2 pib_3 pits4b pc_3 pi_3 pits4 pc_tile3 pct inc_score corte  r2b   year_j num_jueces judge_g rand_level ncrime penatot_m sentl_valid  {
bysort id_hogar_sisben: egen hh_`var't=median(`var') 
bysort idglobal: egen hh_`var't2=median(hh_`var't) if multiplehhid==1
bysort idglobal: replace hh_`var'=hh_`var't2 if hh_`var'==. & hh_`var't2!=.
drop hh_`var't2 hh_`var't
}

rename sisben_sexo_presoh  sex_presoh 
qui tab hh_crime_code, gen (hhccd)
qui tab  hh_year_j, gen(hh_year_jd)
qui tab yob, gen(yobd)
qui tab maxyi, gen(maxyid)
qui tab sisben_year , gen(syd)
areg finaledu  hh_pc_3 pcsq sisben_sexo  both sisben_yd* yobd* hh_inc_score if  son_ofcrim==1  & hh_pits4b<99 &  hh_pits4b>1 & hh_pct<99 &  hh_pct>1 & age_attcrime>=0 & jailk14!=.  & yob>=1990 & yob<=2007 & first_sisben==sisben_year   , a(hh_r2b)
predict res_edu1 if e(sample), resid
lpoly res_edu1 hh_pi_3 if e(sample) , ci yaxis(1) xtitle(PI) title("RF: Incarceration Leniency and Years of Education") ///
noscatter  graphregion(color(white)) bgcolor(white)  level(90) legend(off) ytitle(Years of education, axis(1)) ///
addplot(hist  hh_pi_3 if e(sample) , bin(34)   yaxis(2) bcolor(none))  saving(rfpoly, replace)
graph export rfpoly.png, replace
clear


********************************************************************************
********************************************************************************
********************************************************************************

****************
****Table 1*****
****************


use "data.dta"
gen head=1 if sisben_parentesco==1
replace head=0 if sisben_parentesco!=1 & sisben_parentesco!=.
gen working=1 if sisben_actividad==1
replace working=0 if sisben_actividad!=1 & sisben_actividad!=.
gen studying=1 if sisben_actividad==3
replace studying=0 if sisben_actividad!=3 & sisben_actividad!=.
gen unemp_noac=1 if sisben_actividad==0 | sisben_actividad==2 
replace unemp_noac=0 if sisben_actividad!=0 | sisben_actividad!=2 & sisben_actividad!=.
 gen maleandhhh=0
 replace maleandhhh=1 if sisben_sexo==1  & sisben_parentesco==1
 bysort id_hogar_sisben: egen maleh=max(maleandhhh)
gen yeduc=0 if sisben_niveleduc==0
replace yeduc=sisben_grado if sisben_niveleduc==1 & sisben_grad!=.
replace yeduc=(sisben_grado+5) if sisben_niveleduc==2 & sisben_grad!=.
replace yeduc=(sisben_grado+11) if sisben_niveleduc>2 & sisben_niveleduc!=. & sisben_grad!=.
gen hs=1 if yeduc>=11 & yeduc!=.
replace hs=0 if yeduc <11
gen single =1 if sisben_estcivil==5
replace single=0 if sisben_estcivil==1 | sisben_estcivil==2 | sisben_estcivil==3  | sisben_estcivil==4  
bysort id_hogar_sisben: gen temp=yeduc if sisben_parentesco==1
bysort id_hogar_sisben: egen yeduc_hh=median(yeduc)
drop temp
  
gen temp=0
bysort id_hogar_sisben:replace temp=1 if sisben_parentesco==4 |  sisben_parentesco==5 |  sisben_parentesco==8 | sisben_parentesco==11 | sisben_parentesco==12 | sisben_parentesco==6
bysort id_hogar_sisben:egen mult_gen=max(temp)
bysort id_hogar_sisben: gen persons=_N

eststo clear
 set more off
 eststo: areg crim_recodb yeduc sex  working studying maleh single hs  head sisben_puntaje yob  yeduc_hh  persons mult_gen  i.maxyi  if yob<1990 & last_sisben==sisben_year , a(sisben_codane) 
   gen sample=1 if e(sample)
tabstat  yeduc sex  working studying maleh single hs  head sisben_puntaje yob  yeduc_hh persons mult_gen if sample==1 & yob<1990 & last_sisben==sisben_year    , by(crim_recodb) 
tabstat  yeduc   working studying maleh single hs  head sisben_puntaje yob  yeduc_hh persons mult_gen if sex==1 & sample==1 & yob<1990 & last_sisben==sisben_year    , by(crim_recodb) 
tabstat  yeduc   working studying maleh single hs  head sisben_puntaje yob  yeduc_hh persons mult_gen if sex==0 & sample==1 & yob<1990 & last_sisben==sisben_year    , by(crim_recodb) 
tabstat  yeduc sex  working studying maleh single hs  head sisben_puntaje yob  yeduc_hh persons mult_gen if sample==1 & yob<1990 & 2010==sisben_year    , by(crim_recodb) 
tabstat yeduc if sisben_edad>=15 & sisben_edad<=19 & sample==1  & last_sisben==sisben_year , by (hhcrim_recod)
*****
eststo clear
 set more off
 eststo: areg sent_forjail yeduc sex  working studying maleh single hs  head sisben_puntaje yob  yeduc_hh  persons mult_gen  i.maxyi  if yob<1990 & last_sisben==sisben_year , a(sisben_codane) 
 gen sample=1 if e(sample)
 tabstat  yeduc sex  working studying maleh single hs  head sisben_puntaje yob  yeduc_hh persons mult_gen if sample==1 & yob<1990 & last_sisben==sisben_year  & merge_fam_crime==3 , by(sent_forjail) 
tab sent_forjail if sample==1 & yob<1990 & last_sisben==sisben_year  

clear
****************
****Table 2*****
****************
use "spoa.dta"

egen r2b=group(rand_level year_sent) if rand_level!=. & year_sent!=.

foreach var of varlist r2bd1-r2bd836 {
   egen m_`var'= mean(`var')
   gen dm_m_`var'=`var'-m_`var'
   drop  m_`var' 
   } 
   bysort code_delito: egen conv_score=mean(condenado)
   egen mean_con=mean(condenado)
   egen temp1=mean(conv_score)
   gen dm_con_score=conv_score-temp1
   drop temp1    
 reg condenado dm_m_r2bd* dm_con_score, noconst 
 predict res_ri4, resid
 bysort   judge_merge : egen sumtemp1=total(res_ri4) if e(sample)
bysort   judge_merge : egen count_temp1=count(res_ri4)  if e(sample)
gen pc_3 =(sumtemp1-res_ri4)/(count_temp1-1) 
	  	  eststo clear
	  foreach var in  violent property drugs missd  edad sex ncrime {
qui eststo: areg `var' pc_3     ,  vce(cluster r2b)  a(r2b) 
}
 esttab, r2 ar2  scalars( N_clust )se star( * 0.10 ** 0.05 *** 0.01)  brackets 
   eststo clear
    areg  pc_3  dm_m_cat1 dm_m_cat2 dm_m_cat3 dm_m_cat4 dm_m_cat5 edad sex  ncrime dm_con_score  ,  vce(cluster r2b)  a(r2b) 
   test  dm_m_cat1 dm_m_cat2 dm_m_cat3 dm_m_cat4 dm_m_cat5 edad sex  ncrime 
   	  	 eststo clear
drop r2bd* dm*

merge m:1 judge_merge using   "j&s.dta"

foreach var of varlist r2bd1-r2bd580 {
   egen m_`var'= mean(`var')
   gen dm_m_`var'=`var'-m_`var'
   drop  m_`var' 
   } 

   bysort code_delito: egen inc_score=mean(sent_forjail)
   egen mean_inc=mean(sent_forjail)	
   egen temp1=mean(inc_score)
   gen dm_inc_score=inc_score-temp1
   drop temp1
   
 reg sent_forjail  dm_m_r2bd*  , noconst 
 reg sent_forjail dm_inc_score   dm_m_r2bd*, noconst   
 predict res_r4 if e(sample), resid
 
bysort   judge_merge : egen sumtemp1=total( res_r4) if e(sample)
bysort   judge_merge : egen count_temp1=count( res_r4)  if e(sample)
gen pi_3=(sumtemp1- res_r4)/(count_temp1-1) 
gen pib_3=(sumtemp1)/(count_temp1) 
xtile pits4 =  pi_3  , nq(100)
eststo clear
	  foreach var in  violent property drugs missd  edad sex ncrime {
qui eststo: areg `var' pi_3   inc_score if insample==1 ,  vce(cluster r2b)  a(r2b) 
}
 esttab, r2 ar2  scalars( N_clust )se star( * 0.10 ** 0.05 *** 0.01)  brackets  drop(inc_score)
 eststo clear
 areg  pi_3 inc_score  violent property drugs missd  edad sex  ncrime if insample==1 ,  vce(cluster r2b)  a(r2b) 
 test  violent property drugs missd  edad sex  ncrime 
clear

use "data.dta"

bysort idglobal: egen first_sisben=min(sisben_year)


gen sex=1 if sisben_sexo==1 
replace  sex=0 if sisben_sexo==2 
gen head=1 if sisben_parentesco==1
replace head=0 if sisben_parentesco!=1 & sisben_parentesco!=.


gen working=1 if sisben_actividad==1
replace working=0 if sisben_actividad!=1 & sisben_actividad!=.
gen studying=1 if sisben_actividad==3
replace studying=0 if sisben_actividad!=3 & sisben_actividad!=.
gen unemp_noac=1 if sisben_actividad==0 | sisben_actividad==2 
replace unemp_noac=0 if sisben_actividad!=0 | sisben_actividad!=2 & sisben_actividad!=.
gen maleandhhh=0
 replace maleandhhh=1 if sisben_sexo==1  & sisben_parentesco==1
 bysort id_hogar_sisben: egen maleh=max(maleandhhh)
gen yeduc=0 if sisben_niveleduc==0
replace yeduc=sisben_grado if sisben_niveleduc==1 & sisben_grad!=.
replace yeduc=(sisben_grado+5) if sisben_niveleduc==2 & sisben_grad!=.
replace yeduc=(sisben_grado+11) if sisben_niveleduc>2 & sisben_niveleduc!=. & sisben_grad!=.
gen hs=1 if yeduc>=11 & yeduc!=.
replace hs=0 if yeduc <11
gen single =1 if sisben_estcivil==5
replace single=0 if sisben_estcivil==1 | sisben_estcivil==2 | sisben_estcivil==3  | sisben_estcivil==4  
bysort id_hogar_sisben: gen temp=yeduc if sisben_parentesco==1
bysort id_hogar_sisben: egen yeduc_hh=median(yeduc)
drop temp
gen hs_completed=0 if finaledu<11 & yob>=1988 & yob<=2000
replace hs_completed=1 if finaledu==11 &  yob>=1988 & yob<=2000
gen mcr_hh=1 if peoplecrhhi>1 & peoplecrhhi!=.
replace mcr_hh=0 if peoplecrhhi==1
bysort id_hogar_sisben: egen temp=min(year_j) if  merge_fam_crime==3
drop if ( year_j>temp  & merge_fam_crime==3) 
drop peoplecrhhi peoplecr peoplecrhh
bysort id_hogar_sisben: egen peoplecr=count(id) if  merge_fam_crime==3
bysort id_hogar_sisben: egen peoplecrhh=max(peoplecr) 
bysort idglobal: egen peoplecrhhi=max(peoplecrhh) 
drop temp
bysort id_hogar_sisben: egen temp=min(yearcrime) if  merge_fam_crime==3 
drop if ( yearcrime>temp  & merge_fam_crime==3 & peoplecrhhi>1 & yearcrime!=.) 
drop peoplecrhhi peoplecr peoplecrhh
bysort id_hogar_sisben: egen peoplecr=count(id) if  merge_fam_crime==3
bysort id_hogar_sisben: egen peoplecrhh=max(peoplecr) 
bysort idglobal: egen peoplecrhhi=max(peoplecrhh) 
bysort idglobal: egen miny=min(sisben_year)
bysort idglobal: egen maxy=max(sisben_year)
bysort idglobal: gen both=1 if miny==2005 & maxy==2010
bysort idglobal: replace both=0 if miny!=2005 | maxy!=2010
tab sisben_year, gen (sisben_yd)
foreach var in   datesent1    yearcrime crime_cat crime_code  {
bysort id_hogar_sisben: egen hh_`var'=median(`var') 
}
foreach var in   datesent1   yearcrime crime_cat crime_code {
bysort id_hogar_sisben: egen hh_`var't=median(`var') 
bysort idglobal: egen hh_`var't2=median(hh_`var't) if multiplehhid==1
bysort idglobal: replace hh_`var'=hh_`var't2 if hh_`var'==. & hh_`var't2!=.
}
gen age_attcrime=hh_yearcrime-yob
replace sisben_sexo=0 if sisben_sexo==2
 foreach var in sisben_sexo  yeduc yob  {
gen temp5=`var' if merge_fam_crime==3
bysort id_hogar_sisben: egen `var'_presoh=median(temp5) if    peoplecrhhi==1 
drop temp* 
}
foreach var in sisben_sexo  yeduc yob  {
gen temp5=`var' if merge_fam_crime==3
bysort id_hogar_sisben: egen `var'_presoht=median(temp5) if    peoplecrhhi==1 
bysort idglobal: egen `var'_presoht2=median(`var'_presoht) if multiplehhid==1
bysort idglobal: replace `var'_presoh=`var'_presoht2 if `var'_presoh==. & `var'_presoht2!=.
drop temp* 
}
gen jailk14=1 if gjail0==1 & hh_datesent1<(yob+14)
replace jailk14=0 if gjail0==0 & hh_datesent1<(yob+14)

foreach var in sisben_puntaje   yeduc_hh yeduc   sisben_parentesco sisben_actividad  maleh   {
gen temp`var'=`var' if sisben_year==2005 
bysort idglobal: egen  `var'2005=median(temp`var')
drop  temp`var'
gen temp`var'=`var' if sisben_year==2010  
bysort idglobal: egen  `var'2010=median(temp`var')
drop  temp`var'
}  
egen judge_g= group(judge_merge) if judge_merge!="."

gen crimis_head=0 if merge_fam_crime==3 & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace crimis_head=1 if merge_fam_crime==3 & (sisben_parentesco2005==1  |  sisben_parentesco2010==1 )
gen crimis_spouse=0 if merge_fam_crime==3 & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace crimis_spouse=1 if merge_fam_crime==3 & (sisben_parentesco2005==2  |  sisben_parentesco2010==2 )
gen crimis_son=0 if merge_fam_crime==3 & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace crimis_son=1 if merge_fam_crime==3 & (sisben_parentesco2005==3  |  sisben_parentesco2010==3 )

foreach var in crimis_head crimis_spouse crimis_son {
bysort id_hogar_sisben: egen hh_`var'=median(`var') 
}
 
gen spouse_ofcrim=0 if merge_fam_crime!=3  & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace spouse_ofcrim=1  if merge_fam_crime!=3  & (hh_crimis_head==1 & sisben_parentesco2005==2) | (hh_crimis_spouse==1 & sisben_parentesco2005==1) |  (hh_crimis_son==1 & sisben_parentesco2005==7)
gen son_ofcrim=0 if merge_fam_crime!=3  & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace son_ofcrim=1  if merge_fam_crime!=3  & (hh_crimis_head==1 & (sisben_parentesco2005==3 | sisben_parentesco2010==3) ) | (hh_crimis_spouse==1 & (sisben_parentesco2005==3 | sisben_parentesco2010==3 )) |  (hh_crimis_son==1 & (sisben_parentesco2005==4 | sisben_parentesco2010==4 ))
  xtile pct=pc_3, nq(100)
  tab hh_crime_cat, gen(ccatd)
gen hh_jail=gjail0
gen hh_jail1=gjail1
xtile pc_raw_tile=pc_raw, nq(3)
foreach var in sent_year pc_raw  pi_2 pib_3 pits4b pc_3 pc_1   pct1 pi_3 pits4 pc_tile3 pct inc_score corte  r2b   year_j num_jueces judge_g rand_level ncrime penatot_m sentl_valid {
bysort id_hogar_sisben: egen hh_`var'=median(`var') 
}

foreach var in sent_year pc_raw  pct2_100 pc_tile2 pc_raw100 pc_raw_tile  pi_2 pib_3 pits4b pc_3 pi_3 pits4 pc_tile3 pct inc_score corte  r2b   year_j num_jueces judge_g rand_level ncrime penatot_m sentl_valid  {
bysort id_hogar_sisben: egen hh_`var't=median(`var') 
bysort idglobal: egen hh_`var't2=median(hh_`var't) if multiplehhid==1
bysort idglobal: replace hh_`var'=hh_`var't2 if hh_`var'==. & hh_`var't2!=.
drop hh_`var't2 hh_`var't
}

rename sisben_sexo_presoh  sex_presoh 
qui tab hh_crime_code, gen (hhccd)
qui tab  hh_year_j, gen(hh_year_jd)
qui tab yob, gen(yobd)
qui tab maxyi, gen(maxyid)
qui tab sisben_year , gen(syd)


   eststo clear 
foreach var in  sisben_sexo yeduc sisben_puntaje yob working studying  sisben_yd1 {
    eststo: reghdfe  `var'  pi_3 pc_3 pcsq dm_inc_score if sisben_year==first_sisben & yob<1988 & sent_year>=ysis & insample==1 ,   vce(cluster r2b) a(r2b)
}
  esttab, r2 ar2  scalars( N_clust F )se star( * 0.10 ** 0.05 *** 0.01)  brackets drop(pc_3 pcsq dm_inc_score)
   eststo clear 
    eststo: reghdfe  pi_3 pc_3 pcsq dm_inc_score  sisben_sexo yeduc sisben_puntaje  yob  working studying  sisben_yd1   if sisben_year==first_sisben & insample==1 ,   vce(cluster r2b) a(r2b)
test  sisben_sexo yeduc sisben_puntaje age_attcrime  yob   working studying  sisben_yd1  both
  esttab, r2 ar2  scalars( N_clust F )se star( * 0.10 ** 0.05 *** 0.01)  brackets drop(pc_3 pcsq dm_inc_score)
  
****************
****Table 3*****
****************
use "data.dta"

bysort idglobal: egen first_sisben=min(sisben_year)


gen sex=1 if sisben_sexo==1 
replace  sex=0 if sisben_sexo==2 
gen head=1 if sisben_parentesco==1
replace head=0 if sisben_parentesco!=1 & sisben_parentesco!=.


gen working=1 if sisben_actividad==1
replace working=0 if sisben_actividad!=1 & sisben_actividad!=.
gen studying=1 if sisben_actividad==3
replace studying=0 if sisben_actividad!=3 & sisben_actividad!=.
gen unemp_noac=1 if sisben_actividad==0 | sisben_actividad==2 
replace unemp_noac=0 if sisben_actividad!=0 | sisben_actividad!=2 & sisben_actividad!=.
gen maleandhhh=0
 replace maleandhhh=1 if sisben_sexo==1  & sisben_parentesco==1
 bysort id_hogar_sisben: egen maleh=max(maleandhhh)
gen yeduc=0 if sisben_niveleduc==0
replace yeduc=sisben_grado if sisben_niveleduc==1 & sisben_grad!=.
replace yeduc=(sisben_grado+5) if sisben_niveleduc==2 & sisben_grad!=.
replace yeduc=(sisben_grado+11) if sisben_niveleduc>2 & sisben_niveleduc!=. & sisben_grad!=.
gen hs=1 if yeduc>=11 & yeduc!=.
replace hs=0 if yeduc <11
gen single =1 if sisben_estcivil==5
replace single=0 if sisben_estcivil==1 | sisben_estcivil==2 | sisben_estcivil==3  | sisben_estcivil==4  
bysort id_hogar_sisben: gen temp=yeduc if sisben_parentesco==1
bysort id_hogar_sisben: egen yeduc_hh=median(yeduc)
drop temp
gen hs_completed=0 if finaledu<11 & yob>=1988 & yob<=2000
replace hs_completed=1 if finaledu==11 &  yob>=1988 & yob<=2000
gen mcr_hh=1 if peoplecrhhi>1 & peoplecrhhi!=.
replace mcr_hh=0 if peoplecrhhi==1
bysort id_hogar_sisben: egen temp=min(year_j) if  merge_fam_crime==3
drop if ( year_j>temp  & merge_fam_crime==3) 
drop peoplecrhhi peoplecr peoplecrhh
bysort id_hogar_sisben: egen peoplecr=count(id) if  merge_fam_crime==3
bysort id_hogar_sisben: egen peoplecrhh=max(peoplecr) 
bysort idglobal: egen peoplecrhhi=max(peoplecrhh) 
drop temp
bysort id_hogar_sisben: egen temp=min(yearcrime) if  merge_fam_crime==3 
drop if ( yearcrime>temp  & merge_fam_crime==3 & peoplecrhhi>1 & yearcrime!=.) 
drop peoplecrhhi peoplecr peoplecrhh
bysort id_hogar_sisben: egen peoplecr=count(id) if  merge_fam_crime==3
bysort id_hogar_sisben: egen peoplecrhh=max(peoplecr) 
bysort idglobal: egen peoplecrhhi=max(peoplecrhh) 
bysort idglobal: egen miny=min(sisben_year)
bysort idglobal: egen maxy=max(sisben_year)
bysort idglobal: gen both=1 if miny==2005 & maxy==2010
bysort idglobal: replace both=0 if miny!=2005 | maxy!=2010
tab sisben_year, gen (sisben_yd)
foreach var in   datesent1    yearcrime crime_cat crime_code  {
bysort id_hogar_sisben: egen hh_`var'=median(`var') 
}
foreach var in   datesent1   yearcrime crime_cat crime_code {
bysort id_hogar_sisben: egen hh_`var't=median(`var') 
bysort idglobal: egen hh_`var't2=median(hh_`var't) if multiplehhid==1
bysort idglobal: replace hh_`var'=hh_`var't2 if hh_`var'==. & hh_`var't2!=.
}
gen age_attcrime=hh_yearcrime-yob
replace sisben_sexo=0 if sisben_sexo==2
 foreach var in sisben_sexo  yeduc yob  {
gen temp5=`var' if merge_fam_crime==3
bysort id_hogar_sisben: egen `var'_presoh=median(temp5) if    peoplecrhhi==1 
drop temp* 
}
foreach var in sisben_sexo  yeduc yob  {
gen temp5=`var' if merge_fam_crime==3
bysort id_hogar_sisben: egen `var'_presoht=median(temp5) if    peoplecrhhi==1 
bysort idglobal: egen `var'_presoht2=median(`var'_presoht) if multiplehhid==1
bysort idglobal: replace `var'_presoh=`var'_presoht2 if `var'_presoh==. & `var'_presoht2!=.
drop temp* 
}
gen jailk14=1 if gjail0==1 & hh_datesent1<(yob+14)
replace jailk14=0 if gjail0==0 & hh_datesent1<(yob+14)

foreach var in sisben_puntaje   yeduc_hh yeduc   sisben_parentesco sisben_actividad  maleh   {
gen temp`var'=`var' if sisben_year==2005 
bysort idglobal: egen  `var'2005=median(temp`var')
drop  temp`var'
gen temp`var'=`var' if sisben_year==2010  
bysort idglobal: egen  `var'2010=median(temp`var')
drop  temp`var'
}  
egen judge_g= group(judge_merge) if judge_merge!="."

gen crimis_head=0 if merge_fam_crime==3 & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace crimis_head=1 if merge_fam_crime==3 & (sisben_parentesco2005==1  |  sisben_parentesco2010==1 )
gen crimis_spouse=0 if merge_fam_crime==3 & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace crimis_spouse=1 if merge_fam_crime==3 & (sisben_parentesco2005==2  |  sisben_parentesco2010==2 )
gen crimis_son=0 if merge_fam_crime==3 & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace crimis_son=1 if merge_fam_crime==3 & (sisben_parentesco2005==3  |  sisben_parentesco2010==3 )

foreach var in crimis_head crimis_spouse crimis_son {
bysort id_hogar_sisben: egen hh_`var'=median(`var') 
}
 
gen spouse_ofcrim=0 if merge_fam_crime!=3  & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace spouse_ofcrim=1  if merge_fam_crime!=3  & (hh_crimis_head==1 & sisben_parentesco2005==2) | (hh_crimis_spouse==1 & sisben_parentesco2005==1) |  (hh_crimis_son==1 & sisben_parentesco2005==7)
gen son_ofcrim=0 if merge_fam_crime!=3  & (sisben_parentesco2005!=. | sisben_parentesco2010!=.)
replace son_ofcrim=1  if merge_fam_crime!=3  & (hh_crimis_head==1 & (sisben_parentesco2005==3 | sisben_parentesco2010==3) ) | (hh_crimis_spouse==1 & (sisben_parentesco2005==3 | sisben_parentesco2010==3 )) |  (hh_crimis_son==1 & (sisben_parentesco2005==4 | sisben_parentesco2010==4 ))
  xtile pct=pc_3, nq(100)
  tab hh_crime_cat, gen(ccatd)
gen hh_jail=gjail0
gen hh_jail1=gjail1
xtile pc_raw_tile=pc_raw, nq(3)
foreach var in sent_year pc_raw  pi_2 pib_3 pits4b pc_3 pc_1   pct1 pi_3 pits4 pc_tile3 pct inc_score corte  r2b   year_j num_jueces judge_g rand_level ncrime penatot_m sentl_valid {
bysort id_hogar_sisben: egen hh_`var'=median(`var') 
}

foreach var in sent_year pc_raw  pct2_100 pc_tile2 pc_raw100 pc_raw_tile  pi_2 pib_3 pits4b pc_3 pi_3 pits4 pc_tile3 pct inc_score corte  r2b   year_j num_jueces judge_g rand_level ncrime penatot_m sentl_valid  {
bysort id_hogar_sisben: egen hh_`var't=median(`var') 
bysort idglobal: egen hh_`var't2=median(hh_`var't) if multiplehhid==1
bysort idglobal: replace hh_`var'=hh_`var't2 if hh_`var'==. & hh_`var't2!=.
drop hh_`var't2 hh_`var't
}

rename sisben_sexo_presoh  sex_presoh 
qui tab hh_crime_code, gen (hhccd)
qui tab  hh_year_j, gen(hh_year_jd)
qui tab yob, gen(yobd)
qui tab maxyi, gen(maxyid)
qui tab sisben_year , gen(syd)
 eststo clear
gen pcsq= hh_pc_3*hh_pc_3
gen pccube= hh_pc_3*hh_pc_3*hh_pc_3
xtset hh_r2b
gen tysis=year(sisben_fecha)
bysort idglobal: egen ysis=min(tysis)
egen hhgr2=group(hh_r2b)
xtivreg2 finaledu (jailk14=hh_pi_3) hh_pc_3 pcsq sisben_sexo  both   sisben_yd* yobd* hh_inc_score if  hh_sent_year>=ysis  &  son_ofcrim==1  & age_attcrime>=0 & jailk14!=.  & yob>=1990 & yob<=2007  & first_sisben==sisben_year , fe cluster(hh_r2b  id_hogar_sisben) first partial(sisben_yd* yobd* )
gen fsample=1 if e(sample)


eststo clear
eststo: reg finaledu jailk14   if   fsample==1 & hh_sent_year>=ysis  &  son_ofcrim==1  & age_attcrime>=0 & jailk14!=.  & yob>=1990 & yob<=2007  & first_sisben==sisben_year ,  cluster(hh_r2b )
eststo: reghdfe  finaledu jailk14   hh_pc_3 pcsq sisben_sexo  both   sisben_yd* yobd* hh_inc_score if  fsample==1 & hh_sent_year>=ysis  &   son_ofcrim==1  & age_attcrime>=0 & jailk14!=.  & yob>=1990 & yob<=2007  & first_sisben==sisben_year ,  a(hh_r2b) cluster(hh_r2b )
eststo: reghdfe  finaledu jailk14  sisben_sexo yobd*  both sisben_yd* sisben_puntaje yeduc_hh if  fsample==1 &  hh_sent_year>=ysis  &   son_ofcrim==1  & age_attcrime>=0 & jailk14!=.  & yob>=1990 & yob<=2007  & first_sisben==sisben_year , a(hh_r2b) cluster(hh_r2b )
eststo: reghdfe  jailk14 hh_pi_3 hh_pc_3 pcsq sisben_sexo  both   sisben_yd* yobd* hh_inc_score if fsample==1 & hh_sent_year>=ysis  &  son_ofcrim==1  & age_attcrime>=0 & jailk14!=.  & yob>=1990 & yob<=2007  & first_sisben==sisben_year , a(hh_r2b) cluster(hh_r2b  id_hogar_sisben)  
eststo: reghdfe finaledu hh_pi_3 hh_pc_3 pcsq sisben_sexo  both   sisben_yd* yobd* hh_inc_score if fsample==1 & hh_sent_year>=ysis  &  son_ofcrim==1  & age_attcrime>=0 & jailk14!=.  & yob>=1990 & yob<=2007  & first_sisben==sisben_year , a(hh_r2b) cluster(hh_r2b  id_hogar_sisben)  
eststo: xtivreg2 finaledu (jailk14=hh_pi_3) hh_pc_3 pcsq sisben_sexo  both   sisben_yd* yobd* hh_inc_score if fsample==1 & hh_sent_year>=ysis  &  son_ofcrim==1  & age_attcrime>=0 & jailk14!=.  & yob>=1990 & yob<=2007  & first_sisben==sisben_year , fe cluster(hh_r2b  id_hogar_sisben) first partial(sisben_yd* yobd* )
eststo: ivreg2 finaledu (jailk14=hh_pi_3) hh_pc_3 pcsq sisben_sexo  both   sisben_yd* yobd* hh_inc_score i.hhgr2 if fsample==1 &  hh_sent_year>=ysis  &  son_ofcrim==1  & age_attcrime>=0 & jailk14!=.  & yob>=1990 & yob<=2007  & first_sisben==sisben_year ,   first cluster(hh_r2b id_hogar_sisben)
weakivtest
esttab, r2 ar2  scalars(N_clust) se star( * 0.10 ** 0.05 *** 0.01)  brackets drop (sisben* 6* 5* 4* 3* 2* 1*  7* 8* 9* y* hh_pc_3 pcsq sisben_sexo both)

*****
